

 class GetAddress {
      static key = 'J5JBZ-Y2O3S-XKIOV-6Z54I-ENCGF-QQBI4'

      static getCoderApi = 'https://apis.map.qq.com/ws/geocoder/v1/'
  
      static getLocation  () {
       
      

         return new Promise((resolve, reject) => {
           uni.getLocation({
            complete:(res:any)=>{
         
               let {
                 longitude, latitude
               } = res;
              
               resolve({
                 longitude,
                 latitude
               });
             }
           })
         })
       }

      geocoder(location:object){
         let { longitude,latitude }:any = location;
         let {
           getCoderApi: url,
           key
         } = GetAddress;
         longitude = longitude || '';
         latitude = latitude || '';
         return uni.request({
           url,
           dataType:'json',
      
      
           data: {location: `${latitude},${longitude}`,key}
         })

       }
       async getData(){
       
         let location:any = await GetAddress.getLocation();
         let geocoder:any = await this.geocoder(location);
       
         return geocoder[1] || geocoder[0];
       }


}

class Suggestion extends GetAddress {
     static suggestionApi = 'https://apis.map.qq.com/ws/place/v1/search/';
     static getkey(){
       return super.key;
     }

    getSuggestion(city = '', keyword = '') {
      let key = Suggestion.getkey();
      let url = Suggestion.suggestionApi;
       return uni.request({
         url,
         data: {
           boundary: `region(${city},0)`,
           keyword,
           key
         }
       })
    }
     
    
}

class Distance extends GetAddress {
  static distanceApi = 'https://apis.map.qq.com/ws/distance/v1/';
  static getkey(){
    return super.key;
  }

  getDistance(from:string,to:string,mode="walking"){
      let key = Distance.getkey();
      let url = Distance.distanceApi;
      return uni.request({
        url,

        data: {
          from,
          to,
          mode,
          key
        }
      })  

  }

}	

class addressToLocation extends GetAddress{
  static addressToLocationApi = 'https://apis.map.qq.com/ws/geocoder/v1/';
  static getkey(){
    return super.key;
  }
  getLocation(address:string){
    let key = Distance.getkey();
    let url = Distance.distanceApi;
    return uni.request({
      url,
      data: {
        address,
        key
      }
    }) 
  }
}

export {
  GetAddress,
  Suggestion,
  Distance,
  addressToLocation
} 